//index.js
//获取应用实例
import http from "../../../class/Utils/Http"
import tips from "../../../class/Utils/Tips"
const app = getApp()

Page({
    data: {
      zwtext:'请选择',
      bmtext:'请选择',
    },
    onLoad: function (get) {
        var that = this
        var user = wx.getStorageSync('user');
        that.setData({
            qyid: get.qyid || 6
        })
        console.log(user)
        if (!user) {
            that.setData({
                shouquan: 1
            })
        } else {
            // wx.showLoading({
            //     title: '加载中...',
            // })
            that.shuju()
        }
        console.log(that.data)
    },
    shuju:function () {
        var that = this
        http.req('/Mingpian/bmzw', {qyid: that.data.qyid}, function (res) {
            console.log(res)
            that.setData({
                bmlist: res.bmlist,
                zwlist: res.zwlist,
                title:res.title
            })
        });
    },
    bindGetUserInfo: function (e) {
        var that = this
        user.user(this,function () {
            that.setData({
                shouquan: 0
            })
        })
    },
    bmchange:function (e) {
        console.log(e)
        var that = this
        var index = e.detail.value;
        var bmval = that.data.bmlist[index].name
        var bmid = that.data.bmlist[index].id

        if (bmid == null){
          that.setData({
            bmtext: '请选择',
          })
        }else{
          that.setData({
            bmval: bmval,
            bmid: bmid,
            bmtext:'',
          })
        }
        
    },
    zwchange:function (e) {
        console.log(e)
        var that = this
        var index = e.detail.value;
        var zwval = that.data.zwlist[index].name
        var zwid = that.data.zwlist[index].id

        if (zwid == null) {
          that.setData({
            zwtext: '请选择',
          })
        } else {
          that.setData({
            zwval: zwval,
            zwid: zwid,
            zwtext: '',
          })
        }
        
    },
    chuangjian:function (e) {
        var that = this
        var bmid = that.data.bmid
        var zwid = that.data.zwid
        var qyid = that.data.qyid
        var user = wx.getStorageSync('user');
        var uid = user.userid

        if(!bmid){
            tips.error("请选择 部门")
            return false
        }
        if(!zwid){
            tips.error("请选择 职位")
            return false
        }
        var data = {
            bumen:bmid,
            zw:zwid,
            qyid:qyid,
            userid:uid
        }
        console.log(data)
        http.req("/Mingpian/add_qiye_mingpian",data,function (res) {
            console.log(res)
            if(res.code==1){
                tips.goto("/pages/mingpian/add/add?s=1&id="+res.id)
            }
            if(res.code == '203'){
                tips.modal("企业名片不足,请联系负责人。")
            }
        })
    }

})
